package com.vf.admin.client.dao;

import com.vf.admin.common.db.BaseDao;
import com.vf.admin.common.splitpage.SplitPageBean;
import com.vf.admin.miaodong.entity.AppVerSion;
import com.vf.admin.utils.StringEx;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 版本更新
 */
@Repository
public class AppVerSionDao extends BaseDao<AppVerSion> {
    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    /**
     * 综合查询
     */
    public List<AppVerSion> find(Map<String,Object> paraMap, SplitPageBean splitPage){
        ArrayList<Object> paramList = new ArrayList<Object>();
        StringBuilder sbName = new StringBuilder("select * from app_version where 1=1  ");
        if(!StringEx.isNull(paraMap.get("id"))){
            sbName.append(" and id  =  ("+paraMap.get("id")+") ");
        }
        if(!StringEx.isNullOrLing(paraMap.get("qudaoid"))){
            sbName.append(" and qudaoid  =  ? ");
            paramList.add(paraMap.get("qudaoid"));
        }
        if(!StringEx.isNullOrLing(paraMap.get("updatestate"))){
            sbName.append(" and updatestate  =  ? ");
            paramList.add(paraMap.get("updatestate"));
        }
        if(!StringEx.isNullOrLing(paraMap.get("state"))){
            sbName.append(" and state  =  ? ");
            paramList.add(paraMap.get("state"));
        }
        sbName.append(" order by id desc");
        String sql = sbName.toString();
        return this.queryList(sql,paramList,AppVerSion.class,splitPage);
    }

    /**
     * 查询
     */
    public AppVerSion get(int id){
        if(StringEx.isNullOrLing(id)){
            return null;
        }
        List<Object> paramList = new ArrayList<Object>();
        StringBuilder sbStr = new StringBuilder("select * from app_version  where 1=1");
        sbStr.append(" and id = ? ");
        paramList.add(id);
        String sql = sbStr.toString();
        return this.queryObj(sql,paramList, AppVerSion.class);
    }

    /**
     * 新增或者更新
     * @throws Exception
     */
    public void save(AppVerSion versions){
        if(versions.getId()!=null && versions.getId()>0){
            this.update(versions);
        }else{
            versions.setId(this.add(versions));
        }
    }

    /**
     * 删除
     */
    public void del(int id){
        if (id <= 0) {
            return;
        }
        String sql = "delete  from app_version  where id = ?";
        ArrayList<Object> paramList = new ArrayList<Object>();
        paramList.add(id);
        this.del(sql, paramList);
    }

    /**
     * 查询最新的版本信息  客户端使用
     */
    public AppVerSion latestVersion(Map<String,String> paraMap){
        ArrayList<Object> paramList = new ArrayList<Object>();
        StringBuilder sbName = new StringBuilder("select * from app_version where 1=1  ");
        if(!StringEx.isNullOrLing(paraMap.get("qudaoid"))){
            sbName.append(" and qudaoid  =  ? ");
            paramList.add(paraMap.get("qudaoid"));
        }
        if(!StringEx.isNullOrLing(paraMap.get("state"))){
            sbName.append(" and state  =  ? ");
            paramList.add(paraMap.get("state"));
        }
        sbName.append(" order by  id desc limit 1");
        String sql = sbName.toString();
        return this.queryObj(sql,paramList, AppVerSion.class);
    }


    /**
     * 查询列表（后台）
     * @param paraMap
     * @param splitPage
     * @return
     */
    public List<AppVerSion> list(Map<String,Object> paraMap, SplitPageBean splitPage){
        ArrayList<Object> paramList = new ArrayList<Object>();
        StringBuilder sbName = new StringBuilder("select * from app_version where 1=1  ");

        if(!StringEx.isNullOrLing(paraMap.get("qudaoid"))){
            sbName.append(" and qudaoid  =  ? ");
            paramList.add(paraMap.get("qudaoid"));
        }
        if(!StringEx.isNullOrLing(paraMap.get("updatestate"))){
            sbName.append(" and updatestate  =  ? ");
            paramList.add(paraMap.get("updatestate"));
        }
        if(!StringEx.isNullOrLing(paraMap.get("state"))){
            sbName.append(" and state  =  ? ");
            paramList.add(paraMap.get("state"));
        }
        if(StringEx.isNotNull(paraMap.get("vername"))){
            sbName.append(" and vername like ? ");
            paramList.add("%"+paraMap.get("vername")+"%");
        }
        sbName.append(" order by  id desc");
        String sql = sbName.toString();
        return this.queryList(sql,paramList,AppVerSion.class,splitPage);
    }

}
